﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Configuration;

namespace Hello_World
{
    public partial class cnblogs_AddArticle : System.Web.UI.Page
    {
        readonly string _loginurl = ConfigurationManager.AppSettings["cnblogsloginurl"];
        readonly string _enditurl = ConfigurationManager.AppSettings["cnblogsEnditurl"];
        readonly string _username = ConfigurationManager.AppSettings["cnblogsusername"];
        readonly string _userpwd = ConfigurationManager.AppSettings["cnblogsuserpwd"];

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnAddcsdnlist_Click(object sender, EventArgs e)
        {
            Logincnblogs(1, txtListurl.Text, Urltype.csdn);
        }
        protected void BtnOne_Click(object sender, EventArgs e)
        {
            Logincnblogs(0, txtoneurl.Text,Urltype.csdn);
        }

        protected void Logincnblogs(int type,string url,Urltype urltype)
        {
            //获取页面中的__VIEWSTATE， __EVENTVALIDATION
            var __VIEWSTATE = "/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt";
            //var viewstate1= Regex.Match(loginpagehtml, reg1).Groups[1].Value;
            var __EVENTVALIDATION = "/wEWBQLWwpqPDQLyj/OQAgK3jsrkBALR55GJDgKC3IeGDE1m7t2mGlasoP1Hd9hLaFoI2G05";
            // Regex.Match(loginpagehtml, reg2).Groups[1].Value;
           
            //提交的数据字典
            var dicdata = new Dictionary<string, string>()
              {
                  {"__VIEWSTATE",__VIEWSTATE},
                  {"__EVENTVALIDATION", __EVENTVALIDATION},
                  {"tbUserName", _username},
                  {"tbPassword", _userpwd},
                  {"__EVENTTARGET",""},
                  {"__EVENTARGUMENT", ""},
                  {"btnLogin", "登  录"}
              };

            //拼接提交的数据字典为a=1&b=2格式
            var postdata = dicdata.Select(dic => string.Format("{0}={1}", dic.Key, dic.Value)).Aggregate((a, b) => a + "&" + b);
            var cookies = login(postdata);
            var title = string.Empty;
            var body = string.Empty;
            var tag = string.Empty;
            switch (type)
            {
                case 0:
                    {
                        var onehtml = GetHtmlCode(url);
                        title = GetTitle(onehtml,urltype);                
                        body = GetBody(onehtml,urltype);
                        tag = GetTag(title, urltype);
                
                        AddAricle(cookies, title, body, tag);
                    }
                    break;
                case 1:
                    {                
                        var listurl = Getlisturl(url,urltype);
                        foreach (var oneurl in listurl)
                        {
                            var onehtml = GetHtmlCode(oneurl);
                            title = GetTitle(onehtml, urltype);
                            body = GetBody(onehtml, urltype);
                            tag = GetTag(title, urltype);

                            AddAricle(cookies, title, body, tag);                }
                    }
                    break;
            }
            
            Page.ClientScript.RegisterStartupScript(this.GetType(), "StartUp", "<script language=\"javascript\"> alert(\"Add OK\") </script>");

        }

        private List<string> Getlisturl(string url, Urltype urltype)
        {
            var list = new List<string>();
            var listhtml = string.Empty; 
            if (urltype == Urltype.csdn)
            {
                if (!url.Contains("?viewmode=contents")) url += "?viewmode=contents";
                listhtml=GetHtmlCode(url);
                var stringSeparators = new string[] { "<span class=\"link_title\">" };
                var strings = listhtml.Split(stringSeparators, StringSplitOptions.None);
                foreach (var onehtml in strings)
                {
                    var index = onehtml.IndexOf("<a href=\"");
                    var index1 = onehtml.IndexOf("\">");
                    if (index1 == -1 || index !=0)
                    {
                        continue;
                    }
                    var length = "<a href=\"".ToString().Length;
                    list.Add("http://blog.csdn.net" + onehtml.Substring(index + length, (index1 - index) - length));
                }
            }
            return list;

        }

        private string GetTag(string title,Urltype urltype)
        {
            var tag=string.Empty;
            if (urltype == Urltype.csdn)
            {
                var strtags = new string[] { "html5", "ajax", "wcf", "C#", "java", "sql", "数据库", "oracle", "mysql", "asp.net", "net", "iis", "webbrowser","xml",
                "php","jsp","webservice","it","javascript","c++","android","windowsphone","linux","js","jquery","源码","json","python","linq","css","silverlight","wp7","seo",
                "mvc","3d","视频","delphi"};
                foreach (var strtag in strtags)
                {
                    if (title.ToUpper().Contains(strtag.ToUpper()))
                    {
                        tag += strtag+",";
                    }
                }
                tag = string.IsNullOrEmpty(tag) ? "it其他" : tag.Substring(0, tag.Length - 1);
            }
            return tag;
        }
        private string GetBody(string onehtml, Urltype urltype)
        {
            var body = string.Empty;
            if (urltype == Urltype.csdn)
            {
                var index = onehtml.IndexOf("<div id=\"article_content\" class=\"article_content\">");
                var index1 = onehtml.IndexOf("<div class=\"share_buttons\" id=\"sharePanel\"></div>");
                if (index1 == -1 || index == -1)
                {
                    return "内容未找到";
                }
                var length = "<div id=\"article_content\" class=\"article_content\">".ToString().Length;
                body = onehtml.Substring(index + length, (index1 - index) - length - 8);
            }
            return body;
        }

        private string GetTitle(string onehtml, Urltype urltype)
        {
            var title = string.Empty;
            if (urltype == Urltype.cto || urltype == Urltype.csdn)
            {
                var index = onehtml.IndexOf("<title>");
                var index1 = onehtml.IndexOf(" - ");
                if (index1 == -1 || index == -1)
                {
                    return "标题未找到";
                }
                var length = "<title>".ToString().Length;
                title= onehtml.Substring(index + length, (index1 - index) - length);
            }
            return title;
        }
        void AddAricle(CookieContainer cookies,string title,string body,string tag)
        {

            var sendreq = (HttpWebRequest)WebRequest.Create(_enditurl);
            var __VIEWSTATE = "/wEPDwULLTE3NjgxMzM0MDIPZBYCZg9kFgYCBA8WAh4EVGV4dAWFATxsaW5rIHJlbD0ic3R5bGVzaGVldCIgdHlwZT0idGV4dC9jc3MiIGhyZWY9Imh0dHA6Ly9jb21tb24uY25ibG9ncy5jb20vYmxvZy9jc3MvYWRtaW5fbWluaS5jc3M/cj01Q0U3NkM1QTBCQkJCRDZFMDU1NDBDMTIwRTNDMEM5MCIgLz5kAgYPFgIfAAWFATxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0IiBzcmM9Imh0dHA6Ly9jb21tb24uY25ibG9ncy5jb20vYmxvZy9zY3JpcHQvYWRtaW5fbWluaS5qcz9yPUZFNDE5OEEyMTc0Qjc2RDE1QkZDOTM2RTlDODMyQzg3Ij48L3NjcmlwdD5kAggPZBYCAgEPZBYaZg8PFgIeC05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9yJEVkaXQkQVBPcHRpb25zJEFQU2l0ZUhvbWUkY2hrRGlzcGxheUhvbWVQYWdlBTBFZGl0b3IkRWRpdCRBUE9wdGlvbnMkQVBTaXRlSG9tZSRjYkhvbWVDYW5kaWRhdGUFIUVkaXRvciRFZGl0JEFkdmFuY2VkJGNrYlB1Ymxpc2hlZAUgRWRpdG9yJEVkaXQkQWR2YW5jZWQkY2hrQ29tbWVudHMFMEVkaXRvciRFZGl0JEFkdmFuY2VkJGNoa0Rpc2FibGVBbm9ueW1vdXNDb21tZW50cwUnRWRpdG9yJEVkaXQkQWR2YW5jZWQkY2hrTWFpblN5bmRpY2F0aW9uBSVFZGl0b3IkRWRpdCRBZHZhbmNlZCRjaGtGdWxsVGV4dEluUnNzBR5FZGl0b3IkRWRpdCRBZHZhbmNlZCRjaGtQaW5uZWQFLUVkaXRvciRFZGl0JEFkdmFuY2VkJGNoa0lzT25seUZvclJlZ2lzdGVyVXNlcg==";
            var dicdata = new Dictionary<string, string>()
                    {
                        {"__VIEWSTATE",__VIEWSTATE},            
                        {"__EVENTTARGET",""},          
                        {"__EVENTARGUMENT", ""},
                        {"Editor$Edit$lkbPost", "发布"},
                        {"Editor$Edit$txbTitle", title},
                        {"Editor$Edit$EditorBody", body},
                        {"Editor$Edit$APOptions$APSiteHome$chkDisplayHomePage", "on"},
                        {"Editor$Edit$Advanced$ckbPublished", "on"},
                        {"Editor$Edit$Advanced$chkComments","on"},
                        {"Editor$Edit$Advanced$chkMainSyndication", "on"},
                        {"Editor$Edit$Advanced$txbEntryName",""},
                        {"Editor$Edit$Advanced$txbExcerpt", ""},
                        {"Editor$Edit$Advanced$txbTag",tag},
                        {"Editor$Edit$Advanced$tbEnryPassword", ""}
                    };

            //注意对提交的数据要进行urlencode编码
            var postdata = dicdata.Select(dic => string.Format("{0}={1}", System.Web.HttpUtility.UrlEncode(dic.Key), System.Web.HttpUtility.UrlEncode(dic.Value))).Aggregate((a, b) => a + "&" + b);

            var message = Encoding.UTF8.GetBytes(postdata);
            sendreq.ContentType = "application/x-www-form-urlencoded";
            sendreq.Method = "POST";
            sendreq.ContentLength = message.Length;
            sendreq.CookieContainer = cookies;
            sendreq.GetRequestStream().Write(message, 0, message.Length);
            sendreq.GetRequestStream().Close();
            sendreq.GetResponse();
            sendreq.Abort();//添加多次防止异常


        }
        CookieContainer login(string postdatas)
        {
            var cookies = new CookieContainer();
            var logreq = (HttpWebRequest)WebRequest.Create(_loginurl);
            logreq.CookieContainer = cookies;
            var postdata = Encoding.UTF8.GetBytes(postdatas);

            logreq.Method = "POST";
            logreq.ContentLength = postdata.Length;
            logreq.ContentType = "application/x-www-form-urlencoded";
            logreq.GetRequestStream().Write(postdata, 0, postdata.Length);
            logreq.GetRequestStream().Close();
            logreq.GetResponse();
            return cookies;
        }
        #region 读取网页源码

        private string GetHtmlCode(string url)
        {
            string htmlCode;
            var webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Timeout = 30000;
            webRequest.Method = "GET";
            webRequest.UserAgent = "Mozilla/4.0";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            var webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
            if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
            {
                using (var streamReceive = webResponse.GetResponseStream())
                {
                    using (var zipStream =
                        new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        using (var sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                        {
                            htmlCode = sr.ReadToEnd();
                        }
                    }
                }
            }
            else
            {
                using (var streamReceive = webResponse.GetResponseStream())
                {
                    using (var sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8))
                    {
                        htmlCode = sr.ReadToEnd();
                    }
                }
            }

            return htmlCode;
        }

        #endregion
     
        protected void LBView1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            Cell1.Attributes["class"] = "SelectedTopBorder";
            Cell2.Attributes["class"] = "TopBorder";
            Cell3.Attributes["class"] = "TopBorder";
        }
        protected void LBView2_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            Cell1.Attributes["class"] = "TopBorder";
            Cell2.Attributes["class"] = "SelectedTopBorder";
            Cell3.Attributes["class"] = "TopBorder";
        }
        protected void LBView3_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
            Cell1.Attributes["class"] = "TopBorder";
            Cell2.Attributes["class"] = "TopBorder";
            Cell3.Attributes["class"] = "SelectedTopBorder";
        }

        protected void btnAdd51ctoAdd_Click(object sender, EventArgs e)
        {

        }

        protected void Btn51ctoOne_Click(object sender, EventArgs e)
        {
            Logincnblogs(0, txtoneurl.Text, Urltype.cto);
        }        
    }
    public enum Urltype
    {
        cto,cnblogs,csdn
    }
}